﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace Example5_Collisions.Model
{
    
    class Level
    {
        /// <summary>
        /// different tile types are represented by enum
        /// if the tile has state I normally use a class instead
        /// </summary>
        public enum Tile
        {
            T_EMPTY = 0,
            T_BLOCKED,
            T_TRAP
        };

        //width and heigt
        public const int g_levelWidth  = 100;
        public const int g_levelHeight = 20;

        //the 2D array of tiles
        internal Tile[,] m_tiles = new Tile[g_levelWidth, g_levelHeight];

        internal Level()
        {
            GenerateLevel();
        }

        private void GenerateLevel()
        {
            Random rand = new Random();
            for (int x = 0; x < g_levelWidth; x++)
            {
                for (int y = 0; y < g_levelHeight; y++)
                {
                    m_tiles[x, y] = Tile.T_EMPTY; //set every tile to empty
                    
                    //Add some random blockes
                    if (rand.Next(20) == 0)
                    {
                        m_tiles[x, y] = Tile.T_BLOCKED;
                    }
                }


                
                m_tiles[x, g_levelHeight - 1] = Tile.T_BLOCKED;
                
            }

            

            
        }


        public bool IsCollidingAt(Vector2 a_pos, Vector2 a_size)
        {

            Vector2 topLeft = new Vector2(a_pos.X - a_size.X / 2.0f, a_pos.Y - a_size.Y);
            Vector2 bottomRight = new Vector2(a_pos.X + a_size.X / 2.0f, a_pos.Y);


            for (int x = 0; x < g_levelWidth; x++)
            {
                for (int y = 0; y < g_levelHeight; y++)
                {

                    if (bottomRight.X < (float)x)
                        continue;
                    if (bottomRight.Y < (float)y)
                        continue;
                    if (topLeft.X > (float)x + 1.0f)
                        continue;
                    if (topLeft.Y > (float)y + 1.0f)
                        continue;

                    if (m_tiles[x, y] == Tile.T_BLOCKED)
                    {
                        return true;
                    }
                }
            }


            return false;
        }
    }
}
